<?php

namespace App\Http\Controllers\Store;

use App\Model\Accounts;
use App\Model\Leave;
use App\Model\Sch_desc;
use App\Model\Settle_desc;
use App\Model\Settlement;
use App\Model\Staff_settle;
use App\Model\Staff_task;
use App\Model\Stop_settle;
use App\Model\Workflow;
use App\Model\Workflow_desc;
use App\Model\Workflow_jl;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Mobile\ApprovalController;
//任务结算
class SettleController extends CommonController
{
    //
	protected $settle;
	protected $staff_settle;
	protected $Sch_desc;
	protected $settle_desc;
	protected $Staff_task;
	protected $leave;
	protected $staff_stak;
	protected $Stop_settle;
	protected $Workflow_desc;
	protected static $uid;
	protected $Workflow_jl;
	protected $Workflow;
	public function __construct()
	{
		parent::__construct();

		$this->settle=new Settlement();
		$this->staff_settle=new Staff_settle();
		$this->Sch_desc=new Sch_desc();
		$this->settle_desc=new Settle_desc();
		$this->Staff_task=new Staff_task();
		$this->leave=new Leave();
		$this->Stop_settle=new Stop_settle();
		$this->Workflow_desc=new Workflow_desc();
		$this->Workflow_jl=new Workflow_jl();
		$this->Workflow=new Workflow();
		self::$uid=Auth::user()->id;

	}


//	奖金结算首页
	public function reward_index(){
		$day=Accounts::where(['company_id'=>$this->company_id,'type'=>2,'statu'=>1,'status'=>1])
			->first()->account_day;
//		$data['period']=date("Y-m",time());
//		查询奖金结算日
		try{
			$data=$this->settle->reward_list($this->store_id);
			if(!count($data)){
				return $this->success([]);
			}
			$result['settle_id']=$data['id'];
			$result['date']=date("Y年m月",strtotime($data['period'])).$day.'日';
			$result['status']=$data['status']==4?0:1;
			$result['message']=$data['status']==4?'已提交':'立即提交';
			$result['reward']=$this->leave_reward($data['id']);

			$newArr=[];$str=[];
			foreach($data['settle_desc'] as $kk=>$vv){
				$newArr[$vv['store_station_id']]['data'][]=$vv;
				$newArr[$vv['store_station_id']]['station_name']=$vv['station_name'];
				$newArr[$vv['store_station_id']]['store_id']=$vv['store_id'];
				$newArr[$vv['store_station_id']]['store_station_id']=$vv['store_station_id'];
			}
			foreach ($newArr as $k=>$v){
				$str[]=$v;
			}
//			return $str;
			$result['settle_desc']=$str;
			foreach($result['settle_desc'] as $k=>$v){
				$result['settle_desc'][$k]['count']=0;
				foreach($result['settle_desc'][$k]['data'] as $m=>$n){

					$result['settle_desc'][$k]['count'] += $n['reward'];
				}
				$result['settle_desc'][$k]['distribution']=$this->distribution($v['store_id'],$v['store_station_id']);
				$result['settle_desc'][$k]['num']=count($v['data']);
			}
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
			return $this->failed($message);
		}
	}


//	奖金结算详情
	public function reward_lists(Request $request){
		$store_id=$this->store_id;
		$store_station_id=$request->store_station_id;
		$data=$this->settle_desc->reward_lists($store_id,$store_station_id);
		foreach($data as $kk=>$vv){
			$data[$kk]['sign']=$this->Sch_desc->sign_desc($vv['staff_id']);
		}
		return $this->success($data);
	}



//	终止结算首页
	public function stop_list(){
		try{
			$data=$this->Workflow_desc->stop_list_store(self::$uid);
			foreach($data as $kk=>$vv){
			$firstday = date('Y-m-01', strtotime($vv['created_at'])); //月初
			$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
				$data[$kk]['leave_time']=$this->leave_time($vv['stop_settle']['staff_id'],$firstday,$lastday);
				$data[$kk]['sign_day']=$this->sign_day($vv['stop_settle']['staff_id'],$firstday,$lastday);
			}
			$array=[];
			foreach($data as $k=>$v){
				$array[$k]['approval_id']=$v['id'];
				$array[$k]['id']=$v['stop_id'];
				$array[$k]['sign_day']=$v['sign_day'];
				$array[$k]['leave_time']=$v['leave_time'];
				$array[$k]['staff_info']['name']=$v['stop_settle']['staff_info']['name'];
				$array[$k]['staff_info']['phone']=$v['stop_settle']['staff_info']['phone'];
				$array[$k]['staff_info']['avator']=$v['stop_settle']['staff_info']['avator'];
				$array[$k]['staff_task']['store_station']['name']=$v['stop_settle']['staff_task']['store_station']['name'];

			}
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($array);
		}else{
			return $this->failed($message);
		}
	}


	//终止结算详情
	public function stop_desc(Request $request){
		$id=$request->id;
		$dataDesc=$this->Stop_settle->where(['id'=>$id])->first();
		$firstday = date('Y-m-01', strtotime($dataDesc['created_at'])); //月初
		$lastday = date('Y-m-d', strtotime("$firstday +1 month -1 day"));//月末
		try{
			$result=$this->alltime($dataDesc['staff_task_id'],$firstday,$lastday,$dataDesc['staff_id']);
			$res=$this->leave_desc($dataDesc['staff_task_id'],$firstday,$lastday,$dataDesc['staff_id']);
			$newArr=[];
			foreach($res as $kk=>$vv){
				$newArr[]=date('Y-m-d',strtotime($vv['start_time']));
			}
			$result['leave_day']=$newArr;
			$result['leave_desc']=$res;
			$result['leave_month']=date("Y-m");
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->success($result);
		}else{
			return $this->failed($message);
		}
	}


//	店长端终止申请
	public function stop_approve(Request $request){
		$stop_id=$request->id;
		$status=$request->status;
		$reason=$request->reason;
//		查询申请工作流
		$workflow_descData=$this->Workflow_desc->searchWorkflow(self::$uid,$stop_id);

		DB::beginTransaction();
		try{
//			添加申请记录
			$workflow_jl['workflow_id']=$workflow_descData['id'];
			$workflow_jl['workflow_id']=$workflow_descData['lovel_uid'];
			$workflow_jl['status']=$status;
			$workflow_jl['reason']=$reason;
			$this->Workflow_jl->create($workflow_jl);
//			修改申请流程申请表状态
			$this->where(['id'=>$workflow_descData['id']])->update(['status'=>$status]);
//			查看有没有下级申请,有则添加申请流程

			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if(!isset($message)){
			return $this->success();
		}else{
			return $this->failed($message);
		}
	}

//	审核终止结算
	public function approve_stop(Request $request){
		$this->validate($request,[
//			'uid'=>'required',
			'approval_id'=>'required',
			'status'=>'required',
		]);
		$approve=new ApprovalController();
		$uid=self::$uid;
		$approval_id=$request->approval_id;
		$status=$request->status;
		$reason=isset($request->reason)?$request->reason:null;
		DB::beginTransaction();
		try{
			$approve->is_approval($uid,$approval_id,$status,$reason);
			DB::commit();
		}catch(\Exception $exception){
			DB::rollBack();
		}

		if(!isset($message)){
			return ['code'=>200,'message'=>'审批成功'];
		}else{
			return $this->failed($message);
		}


	}


//	判断分配情况
	public function distribution($store_id,$store_station_id){
//		$Staff_settle=new Staff_settle();
		$data=$this->settle_desc->where(['store_station_id'=>$store_station_id])->get();
		$i=0;$j=0;
		foreach($data as $kk=>$vv){
			if($vv['reward']==null){
				$i++;
			}else{
				$j++;
			}
		}
		if($j==0){
			return '未分配';
		}elseif($i==0){
			return '已分配';
		}else{
			return '部分分配';
		}
	}

//	奖金分配
	public function bonus_shares(Request $request){
		$settle_desc_id=$request->settle_desc_id;
		$reward=$request->reward;
		try{
			$standard_money=$this->settle_desc->where(['id'=>$settle_desc_id])->first()->standard_money;
			$result=$this->settle_desc->where(['id'=>$settle_desc_id])
				->update(['reward'=>$reward,'actual_money'=>$standard_money+$reward,'status'=>4]);
		}catch (\Exception $exception){
			$message=$exception->getMessage();
		}
		if(!isset($message)){
			return $this->updated($result);
		}else{
			return $this->failed($message);
		}
	}


//	剩余分配奖金额度
	public function surplus(Request $request){
		$settle_id=$request->settle_id;
		$result=$this->leave_reward($settle_id);
		return $this->updated($result);
	}

//	查询剩余分配奖金
	public function leave_reward($settle_id){
		$sum_reward=Settlement::where(['id'=>$settle_id])->first()->reward;
		$part=Settle_desc::where(['settle_id'=>$settle_id])->sum('reward');
		return $sum_reward-$part;
	}

//   提交奖金分配
	public function submit(Request $request){
		$settle_id=$request->settle_id;
		$undistributed=$this->leave_reward($settle_id);
		DB::beginTransaction();
		try{
			$result=Settlement::where(['id'=>$settle_id])->update(['undistributed'=>$undistributed,'status'=>4]);
			DB::commit();
		}catch (\Exception $exception){
			$message=$exception->getMessage();
			DB::rollBack();
		}
		if($result){
			$data['code']=200;
			$data['message']='提交成功';
		}else{
			$data['code']=400;
			$data['message']='提交失败';
		}
		if(!isset($message)){
			return $this->updated($data);
		}else{
			return $this->failed($message);
		}
	}


	//中止时间
	public function leave_time($staff_id){
		return $this->leave->where(['staff_id'=>$staff_id])->sum('time');
	}

//	签到情况
	public function sign_day($staff_id,$start_day,$end_day){
		$sign=$this->Sch_desc
			->whereBetween('time',[$start_day,$end_day])
			->where(function ($query) use($staff_id){
				$query->where(['staff_id'=>$staff_id]);
				$query->whereNotNull('sign_start');
				$query->whereNotNull('sign_end');
			})->get();

		$all=$this->Sch_desc
			->whereBetween('time',[$start_day,$end_day])
			->where(function ($query) use($staff_id){
				$query->where(['staff_id'=>$staff_id]);
			})->get();
		return count($sign).'/'.count($all);
	}



	public function alltime($id,$start_time,$end_time,$staff_id){
		return $this->Sch_desc->alltime($id,$start_time,$end_time,$staff_id);
	}


//	中止详情
	public function leave_desc($id,$firstday,$lastday,$staff_id){
		return $this->leave->stop_desc($id,$firstday,$lastday,$staff_id);
	}








}
